package com.leihao.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leihao.common.core.domain.R;
import com.leihao.common.core.exception.ServiceException;
import com.leihao.common.core.utils.StringUtils;
import com.leihao.product.domian.FirstProductType;
import com.leihao.product.domian.ProductType;
import com.leihao.product.domian.ProductTypeLinkage;
import com.leihao.product.domian.listRequest.TypeListRequest;
import com.leihao.product.mapper.ProductTypeMapper;
import com.leihao.product.remote.LeiHaoFileRemoteService;
import com.leihao.product.service.ProductTypeService;
import com.leihao.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductTypeServiceImpl implements ProductTypeService {


    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Autowired
    private LeiHaoFileRemoteService fileRemoteService;

    @Override
    public List<ProductTypeLinkage> findLinkageList() {
        List<ProductTypeLinkage> list = productTypeMapper.findAll();
        List<ProductTypeLinkage> collect = list.stream().filter(productTypeLinkage -> productTypeLinkage.getParentId() == 0)
                .map(productTypeLinkage -> {
                    productTypeLinkage.setList(getParentList(list, productTypeLinkage.getProductTypeId()));
                    return productTypeLinkage;
                }).collect(Collectors.toList());
        return collect;
    }

    public List<ProductTypeLinkage> getParentList(List<ProductTypeLinkage> list, Integer productTypeId) {
        List<ProductTypeLinkage> collect = list.stream().filter(productTypeLinkage -> productTypeLinkage.getParentId() == productTypeId)
                .map(productTypeLinkage -> {
                    if (productTypeLinkage != null){
                        productTypeLinkage.setList(getParentList(list, productTypeLinkage.getProductTypeId()));
                    }
                    return productTypeLinkage;
                }).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect) || collect.size() == 0) {
            return null;
        }
        return collect;
    }

    /**
     * 查询所有一级商品分类
     *
     * @param typeListRequest
     * @return
     */
    @Override
    public PageInfo<ProductType> findFirstList(TypeListRequest typeListRequest) {
        if (StringUtils.isNull(typeListRequest)) {
            throw new ServiceException("参数异常!,不能为空", 400);
        }
        if (typeListRequest.getParentId() == null) {
            typeListRequest.setParentId(0);
        }
        PageHelper.startPage(typeListRequest.getPageNum(), typeListRequest.getPageSize());
        List<ProductType> firstList = productTypeMapper.findFirstList(typeListRequest);
        firstList.stream().forEach(productType -> {
            if (productType.getParentId() == 0){
                productType.setTypeRank("一级");
            }else if (productType.getParentId() != 0){
                productType.setTypeRank("二级");
            }

        });
        PageInfo<ProductType> productTypePageInfo = new PageInfo(firstList);
        return productTypePageInfo;
    }

    /**
     * 查询对应编号下所有的二级分类
     *
     * @param typeListRequest
     * @return
     */
    @Override
    public PageInfo<ProductType> findTwoList(TypeListRequest typeListRequest) {
        if (StringUtils.isNull(typeListRequest)) {
            throw new ServiceException("参数异常!", 500);
        }
        PageHelper.startPage(typeListRequest.getPageNum(), typeListRequest.getPageSize());
        List<ProductType> firstList = productTypeMapper.findFirstList(typeListRequest);
        PageInfo<ProductType> productTypePageInfo = new PageInfo(firstList);
        return productTypePageInfo;
    }

    @Override
    public TypeListRequest getUpList(Integer parenId) {
        if (parenId == null) {
            throw new ServiceException("参数异常!");
        }
        ProductType findById = productTypeMapper.findById(parenId);
        if (findById == null) {
            throw new ServiceException("该分类没有上级!");
        }
        TypeListRequest typeListRequest = new TypeListRequest();
        typeListRequest.setParentId(findById.getParentId());
        typeListRequest.setPageNum(1);
        typeListRequest.setPageSize(5);
        return typeListRequest;
    }

    @Override
    public void delete(Integer productTypeId) {
        if (productTypeId == null || productTypeId <= 0) {
            throw new ServiceException("参数异常!", 500);
        }
        productTypeMapper.delete(productTypeId);
    }

    /**
     * 修改商品分类
     *
     * @param productType
     * @return
     */
    @Override
    public void update(ProductType productType) {
        if (StringUtils.isNull(productType)) {
            throw new ServiceException("参数异常!", 500);
        }
        if (productType.getProductTypeId() == null || productType.getProductTypeId() <= 0) {
            throw new ServiceException("参数异常!", 500);
        }
        productTypeMapper.update(productType);
    }

    /**
     * 添加商品分类
     *
     * @param productType
     * @return
     */
    @Override
    public void add(ProductType productType) {
        if (StringUtils.isNull(productType)) {
            throw new ServiceException("参数异常!", 500);
        }
        productTypeMapper.add(productType);
    }

    @Override
    public ProductType findById(Integer productTypeId) {
        if (productTypeId == null || productTypeId <= 0) {
            throw new ServiceException("参数异常!", 500);
        }
        return productTypeMapper.findById(productTypeId);
    }

    @Override
    public List<FirstProductType> selectFirstList() {

        FirstProductType firstProductType = new FirstProductType(0, "无上级分类");
        List<FirstProductType> list = productTypeMapper.selectFirstList();
        list.add(0, firstProductType);
        return list;
    }

}
